#!/bin/bash

# 提取x86-instructions.txt中的所有指令
cat x86-instructions.txt | grep -v '^#' | grep -v '^$' | grep -v '^##' | awk '{print $1}' > all_instructions.txt

# 提取blink-ts中已实现的指令函数
awk '/private execute[A-Z][a-zA-Z]*/ {print $2}' src/executor/InstructionExecutor.ts | sed 's/execute//' > implemented_funcs.txt

# 处理函数名，提取指令名
# 处理特殊情况并规范化指令名
cat implemented_funcs.txt | \
  sed 's/RmR//' | sed 's/RRm//' | sed 's/Rm//' | sed 's/RmToR//' | sed 's/RToRm//' | \
  sed 's/Imm//' | sed 's/Cl//' | sed 's/R[0-9]*//' | \
  sed 's/Immediate//' | sed 's/Register//' | sed 's/Relative//' | \
  sed 's/Into//' | sed 's/Sx//' | sed 's/Dx//' | sed 's/Al//' | sed 's/Ax//' | \
  sed 's/Eax//' | sed 's/Edx//' | sed 's/Reg//' | sed 's/Mem//' | sed 's/Mc//' | \
  sed 's/FlagsRegister//' | sed 's/Address//' | sed 's/Index//' | \
  sed 's/8//' | sed 's/16//' | sed 's/32//' | sed 's/64//' | \
  sed 's/Relative//' | sed 's/BitOp/Bit/' | sed 's/Int3/Int/' | sed 's/Xgetbv/XGETBV/' | sed 's/Xsetbv/XSETBV/' | \
  sed 's/Syscall/SYSCALL/' | sed 's/Wbinvd/WBINVD/' | sed 's/Cpuid/CPUID/' | \
  sed 's/Movsb/MOVSB/' | sed 's/Movs/MOVS/' | sed 's/Cmpsb/CMPSB/' | sed 's/Scasb/SCASB/' | \
  sed 's/Stosb/STOSB/' | sed 's/Lodsb/LODSB/' | sed 's/Cmps/CMPS/' | sed 's/Scas/SCAS/' | \
  sed 's/Stos/STOS/' | sed 's/Lods/LODS/' | \
  tr '[:lower:]' '[:upper:]' | sort | uniq > implemented_instructions.txt

# 手动添加一些特殊情况
# 这些指令的函数名与指令名不完全对应
cat >> implemented_instructions.txt << EOF
ADD
SUB
MUL
IMUL
DIV
IDIV
CMP
TEST
NEG
NOT
INC
DEC
MOV
MOVZX
MOVSX
PUSH
POP
XCHG
AND
OR
XOR
SHL
SHR
SAL
SAR
ROL
ROR
RCL
RCR
JMP
CALL
RET
INT
IRET
IN
OUT
HLT
NOP
JZ
JNZ
JLE
JG
JGE
JL
JBE
JA
JCXZ
CLC
STC
CMC
CLI
STI
CLD
STD
PUSHF
POPF
LAHF
SAHF
ENTER
LEAVE
XADD
BSWAP
EOF

# 排序并去重
sort implemented_instructions.txt | uniq > implemented_unique.txt
mv implemented_unique.txt implemented_instructions.txt

# 找出未实现的指令
comm -23 <(sort all_instructions.txt) <(sort implemented_instructions.txt) > unimplemented_instructions.txt

# 清理临时文件
rm all_instructions.txt implemented_funcs.txt implemented_instructions.txt

# 显示结果
cat unimplemented_instructions.txt